﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPushButton>
#include <QGridLayout>
#include "subwindow_image.h"
#include "newcanvasdlg.h"
#include "mainscene.h"
#include <QButtonGroup>

CANVAS_OPERATION g_currentOperation = CANVAS_OPERATION_CHOOSE;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_area = new QMdiArea();
    InitToolBar();
    //m_area->setViewMode(QMdiArea::TabbedView);
    //m_area->setTabsClosable(true);
    this->setCentralWidget(m_area);
    this->setWindowTitle(QStringLiteral("媒体之巅[多媒体处理综合应用程序]"));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitToolBar()
{
    QGridLayout *toolLayout = new QGridLayout();
    int btnNum = 11;
    QString btnInfo[11][3] = {
        {QStringLiteral("选择"),":/ttom/ui/images/cursor-96.png","CHOOSE"},
        {QStringLiteral("钢笔"),":/ttom/ui/images/pen-96.png","PEN"},
        {QStringLiteral("铅笔"),":/ttom/ui/images/drawing-96.png","PENCIL"},
        {QStringLiteral("线条"),":/ttom/ui/images/line-90.png","LINE"},
        {QStringLiteral("圆形"),":/ttom/ui/images/circle-67.png","ELLIPSE"},
        {QStringLiteral("正方形"),":/ttom/ui/images/square-67.png","SQUARE"},
        {QStringLiteral("三角形"),":/ttom/ui/images/Painbrush.png","TRAINGLE"},
        {QStringLiteral("菱形"),":/ttom/ui/images/Painbrush.png","CHOOSE"},
        {QStringLiteral("长方形"),":/ttom/ui/images/Painbrush.png","CHOOSE"},
        {QStringLiteral("六边形"),":/ttom/ui/images/Painbrush.png","CHOOSE"},
    };
    toolLayout->setAlignment(Qt::AlignTop);
    QButtonGroup *group = new QButtonGroup(this);
    for(int i=0;i<btnNum;i++){
        qDebug() << btnInfo[i][1];
        QPushButton *button = new QPushButton(QIcon(btnInfo[i][1]), tr(""), this);
        button->setCheckable(true);
        button->setToolTip(btnInfo[i][0]);
        button->setObjectName(btnInfo[i][2]);
        group->addButton(button);
        toolLayout->addWidget(button, i/2, i%2);
        connect(button,SIGNAL(pressed()),this,SLOT(onCanvasButtonClick()));
    }
    if(group->buttons().size()){
        group->buttons()[0]->setChecked(true);
    }

    m_drawPanel = new QDockWidget( QStringLiteral("工具栏"),this);
    QWidget *toolWidget = new QWidget(m_drawPanel);
    toolWidget->setLayout(toolLayout);
    m_drawPanel->setWidget(toolWidget);

    m_drawPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::TopDockWidgetArea);
    this->addDockWidget(Qt::LeftDockWidgetArea,m_drawPanel);
}

void MainWindow::on_action_newimage_triggered()
{
    NewCanvasDlg dlg;
    if(dlg.exec()==QDialog::Rejected){
        return;
    }
    MainScene* scene = new MainScene();
    scene->setSceneRect(0,0,dlg.m_iWidth,dlg.m_iHeight);
    SubWindow_Image* subWindow = new SubWindow_Image(scene);
    subWindow->setAttribute(Qt::WA_DeleteOnClose);
    subWindow->resize(640,480);
    m_area->addSubWindow(subWindow);
    subWindow->show();
    qDebug() <<  "newimage";
}

void MainWindow::onCanvasButtonClick()
{
    QObject *obj = sender();
    if(obj->objectName()=="CHOOSE"){
        g_currentOperation = CANVAS_OPERATION_CHOOSE;
    }else if(obj->objectName()=="PEN"){
        g_currentOperation = CANVAS_OPERATION_PEN;
    }else if(obj->objectName()=="PENCIL"){
        g_currentOperation = CANVAS_OPERATION_PENCIL;
    }else if(obj->objectName()=="LINE"){
        g_currentOperation = CANVAS_OPERATION_LINE;
    }else if(obj->objectName()=="SQUARE"){
        g_currentOperation = CANVAS_OPERATION_SQUARE;
    }else if(obj->objectName()=="ELLIPSE"){
        g_currentOperation = CANVAS_OPERATION_ELLIPSE;
    }else if(obj->objectName()=="TRAINGLE"){
        g_currentOperation = CANVAS_OPERATION_TRAINGLE;
    }
    qDebug() << obj->objectName() <<  g_currentOperation;
}
